# SmartCity-LLM Trainer Backend Makefile

.PHONY: help install dev test lint format clean docker-build docker-up docker-down migrate

# 默认目标
help:
	@echo "SmartCity-LLM Trainer Backend 开发命令:"
	@echo "  install     - 安装项目依赖"
	@echo "  dev         - 启动开发服务器"
	@echo "  test        - 运行测试"
	@echo "  lint        - 代码质量检查"
	@echo "  format      - 格式化代码"
	@echo "  clean       - 清理临时文件"
	@echo "  docker-build - 构建Docker镜像"
	@echo "  docker-up   - 启动Docker开发环境"
	@echo "  docker-down - 停止Docker开发环境"
	@echo "  migrate     - 运行数据库迁移"

# 安装依赖
install:
	@echo "安装Python依赖..."
	pip install -r requirements.txt
	@echo "依赖安装完成!"

# 启动开发服务器
dev:
	@echo "启动FastAPI开发服务器..."
	uvicorn main:app --host 0.0.0.0 --port 8000 --reload

# 运行测试
test:
	@echo "运行测试套件..."
	pytest -v --cov=app --cov-report=html

# 代码质量检查
lint:
	@echo "运行代码质量检查..."
	flake8 app/
	mypy app/
	black --check app/
	isort --check-only app/

# 格式化代码
format:
	@echo "格式化代码..."
	black app/
	isort app/
	@echo "代码格式化完成!"

# 清理临时文件
clean:
	@echo "清理临时文件..."
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf .coverage htmlcov/ .pytest_cache/ .mypy_cache/
	@echo "清理完成!"

# 构建Docker镜像
docker-build:
	@echo "构建Docker镜像..."
	docker build -t smartcity-llm-backend .

# 启动Docker开发环境
docker-up:
	@echo "启动Docker开发环境..."
	docker-compose up -d
	@echo "开发环境已启动!"
	@echo "API服务: http://localhost:8000"
	@echo "API文档: http://localhost:8000/api/v1/docs"
	@echo "PostgreSQL: localhost:5432"
	@echo "Redis: localhost:6379"
	@echo "MinIO: http://localhost:9001"

# 停止Docker开发环境
docker-down:
	@echo "停止Docker开发环境..."
	docker-compose down
	@echo "开发环境已停止!"

# 运行数据库迁移
migrate:
	@echo "运行数据库迁移..."
	alembic upgrade head
	@echo "数据库迁移完成!"

# 创建新的数据库迁移
migration:
	@echo "创建新的数据库迁移..."
	@read -p "输入迁移描述: " desc; \
	alembic revision --autogenerate -m "$$desc"

# 重置数据库
reset-db:
	@echo "重置数据库..."
	docker-compose exec postgres psql -U smartcity -d smartcity_llm -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
	alembic upgrade head
	@echo "数据库重置完成!"

# 查看日志
logs:
	@echo "查看应用日志..."
	docker-compose logs -f backend

# 进入后端容器
shell:
	@echo "进入后端容器..."
	docker-compose exec backend bash

# 安装开发环境
setup-dev: install
	@echo "设置开发环境..."
	cp .env.example .env
	@echo "请编辑 .env 文件配置您的环境变量"
	@echo "然后运行: make docker-up"